package com.ruoyi.monthlyInventory.service.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.monthlyInventory.domain.MonthlyInventory;
import com.ruoyi.monthlyInventory.domain.MonthlyInventoryLog;
import com.ruoyi.monthlyInventory.mapper.MonthlyInventoryMapper;
import com.ruoyi.monthlyInventory.service.MonthlyInventoryService;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MonthlyInventoryImpl implements MonthlyInventoryService {

    @Autowired
    private MonthlyInventoryMapper monthlyInventoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private WarehousingMapper warehousingMapper;

    @Autowired
    private AuditMapper auditMapper;

    /**
     * 查询月度管理管理列表
     *
     * @param monthlyInventory 月度管理
     * @return 月度管理集合
     */
    @Override
    public List<MonthlyInventory> selectMonthlyInventoryList(MonthlyInventory monthlyInventory)
    {
        if(monthlyInventory.getYearMonthNyt()==null||monthlyInventory.getYearMonthNyt()==""){
            Date date= DateUtils.getNowDate();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
            String dateString = sdf.format(date);
            monthlyInventory.setYearMonthNyt(dateString);
            monthlyInventory.setCompanyId(ShiroUtils.getSysUser().getCompanyId());
        }else {
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
//            String dateString = sdf.format(monthlyInventory.getYearMonth());
//            monthlyInventory.setYearMonth(dateString);
            monthlyInventory.setCompanyId(ShiroUtils.getSysUser().getCompanyId());
        }
        return monthlyInventoryMapper.selectMonthlyInventoryList(monthlyInventory);
    }

    /**
     * 查询月度管理管理列表
     *
     * @param monthlyInventoryLog 月度管理
     * @return 月度管理集合
     */
    @Override
    public List<MonthlyInventoryLog> selectMonthlyInventoryLogList(MonthlyInventoryLog monthlyInventoryLog)
    {
        return monthlyInventoryMapper.selectMonthlyInventoryLogList(monthlyInventoryLog);
    }

    /**
     * 查询月度管理管理列表
     *
     * @param logId 月度管理
     * @return 月度管理集合
     */
    @Override
    public MonthlyInventoryLog selectMonthlyInventoryLogById(Long logId)
    {
        return monthlyInventoryMapper.selectMonthlyInventoryLogById(logId);
    }


    /**
     * 查询月度管理管理列表
     *
     * @param monthlyInventoryId 月度管理
     * @return 月度管理集合
     */
    @Override
    public MonthlyInventory selectMonthlyInventoryById(Long monthlyInventoryId)
    {
        return monthlyInventoryMapper.selectMonthlyInventoryById(monthlyInventoryId);
    }


    /**
     * 插入月度管理管理列表
     *
     * @param monthlyInventory 月度管理
     * @return 月度管理集合
     */
    @Transactional
    @Override
    public int insertMonthlyInventorys(MonthlyInventory monthlyInventory)
    {
        List<MonthlyInventory> monthlyInventoryList=new ArrayList<>();
        List<MonthlyInventory> monthlyInventoryList2=selectMonthlyInventoryList(new MonthlyInventory());
        Warehousing warehousing=new Warehousing();
        warehousing.setDeptId(ShiroUtils.getSysUser().getCompanyId());
        List<Warehousing> warehousingList=warehousingMapper.selectWarehousingToMonth(warehousing);
        int num=0;
        if(monthlyInventoryList2.size()!=0){
            new AjaxResult(AjaxResult.Type.ERROR, "您已生成月度盘点，请勿重复生成！");
            throw new RuntimeException("您已生成月度盘点，请勿重复生成！");
        }else{
            if(warehousingList.size()!=0){
                for(Warehousing warehousing1:warehousingList){
                    MonthlyInventory monthlyInventory1=new MonthlyInventory();
                    monthlyInventory1.setCompanyId(warehousing1.getDeptId());
                    monthlyInventory1.setBatch(warehousing1.getBatch());
                    monthlyInventory1.setModelNo(warehousing1.getProductType());
                    monthlyInventory1.setProductId(warehousing1.getProductId());
                    monthlyInventory1.setWarehousingNum(warehousing1.getWarehousingNum());
                    Date date= DateUtils.getNowDate();
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
                    String dateString = sdf.format(date);
                    monthlyInventory1.setYearMonthNyt(dateString);
                    monthlyInventory1.setMonthlyInventoryStatus(0);
                    monthlyInventoryList.add(monthlyInventory1);
                }
                num=monthlyInventoryMapper.insertMonthlyInventorys(monthlyInventoryList);
            }else {
                new AjaxResult(AjaxResult.Type.ERROR, "您没有库存,无法生成月度盘点");
                throw new RuntimeException("您没有库存,无法生成月度盘点");
            }
        }
        return num;
    }

    /**
     * 插入月度管理申请
     *
     * @param monthlyInventoryLog 需要插入的数据
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMonthlyInventoryLog(MonthlyInventoryLog monthlyInventoryLog)
    {
        monthlyInventoryLog.setAuditStatus("0");
        monthlyInventoryLog.setCreateBy(ShiroUtils.getSysUser().getUserName());
        monthlyInventoryMapper.insertMonthlyInventoryLog(monthlyInventoryLog);
        Audit audit = new Audit();
        audit.setOrderId(monthlyInventoryLog.getLogId());
        audit.setCompanyId(ShiroUtils.getSysUser().getCompanyId());
        audit.setOrderType("4");
        audit.setAuditStatus("0");
        int num=auditMapper.insertAudit(audit);
        return num;
    }

    /**
     * 更新月度盘点状态
     *
     * @param monthlyInventory 月度盘点
     * @return 月度管理集合
     */
    @Override
    public int updateMonthlyInventory(MonthlyInventory monthlyInventory)
    {
        return monthlyInventoryMapper.updateMonthlyInventory(monthlyInventory);
    }
}
